
package com.baijia.jigou.principal;

import java.util.Map;

import com.baijia.authentication.AccountType;
import com.baijia.authentication.principal.Credentials;
import com.baijia.authentication.principal.resolver.AccountTypeCredentialsToPrincipalResolver;
import com.baijia.jigou.util.WebContext;
import com.baijia.jigou.util.WebContext.WebSession;
import com.google.common.collect.Maps;

public class CookieJigouPrincipalResolver extends AccountTypeCredentialsToPrincipalResolver {
    
    @Override
    public int getAccountType() {
        return AccountType.JIGOU.getCode();
    }

    @Override
    public boolean supports(Credentials credentials) {
        return credentials instanceof CookieJigouCredentials;
    }

    @Override
    protected String extractPrincipalId(Credentials credentials) {
        CookieJigouCredentials cookieCredentials = (CookieJigouCredentials) credentials;
        return cookieCredentials.getSessionId();
    }

    @Override
    protected Map<String, Object> generateAttributes(Credentials credentials) {
        CookieJigouCredentials jigouCredentials = (CookieJigouCredentials) credentials;
        String sessionId = jigouCredentials.getSessionId();
        
//        OrgWebSession webSession = null;
//        try {
//            webSession = (OrgWebSession) OrgMemcachedUtil.getBySessionId(sessionId);
//        } catch (TimeoutException | InterruptedException | MemcachedException e) {
//            logger.error("get OrgWebSession from cookie error!", e);
//        }
        WebSession webSession = WebContext.getSession();
        logger.info("CookieJigouPrincipalResolver webSession:"+webSession);
        Map<String, Object> attributes = Maps.newHashMap();
        if(webSession != null){
            attributes.put(ACCOUNT_ID, webSession.getLoginAccount().getNumber());
        }
        
        return attributes;
    }

}

    